package org.csu.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.cms.dao.CategoryMapper;
import org.csu.cms.dao.ItemMapper;
import org.csu.cms.dao.ProductMapper;
import org.csu.cms.domain.Category;
import org.csu.cms.domain.Item;
import org.csu.cms.domain.Product;
import org.csu.cms.result.Code;
import org.csu.cms.result.Result;
import org.csu.cms.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service("CatalogService")
public class CatalogServiceImpl implements CatalogService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ItemMapper itemMapper;

    @Override
    public Result getCategoryList() {
        List<Category> categoryList=categoryMapper.selectList(null);
        if(categoryList.isEmpty()){
            return new Result(Code.GET_ERR, null, "查询错误，数据库中没有Category列表");
        }
        return new Result(Code.GET_OK, categoryList);
    }

    @Override
    public Result getCategoryById(String categoryId) {
        Category category=categoryMapper.selectById(categoryId);
        if(category == null){
            return new Result(Code.GET_ERR, null, "查找错误，数据库中没有id为"+categoryId+"的category数据");
        }
        return new Result(Code.GET_OK, category);
    }

    @Override
    public Result insertCategory(Category category) {
        int result = categoryMapper.insert(category);
        if(result == 0){
            return new Result(Code.SAVE_ERR, null, "添加失败，受影响行数为0");
        }
        return new Result(Code.SAVE_OK, result);
    }

    @Override
    public Result updateCategory(Category category) {
        System.out.println(category);
        int result = categoryMapper.updateById(category);
        if(result == 0){
            return new Result(Code.UPDATE_ERR, null, "添加失败，受影响行数为0");
        }
        return new Result(Code.UPDATE_OK, result);
    }

    @Override
    public Result deleteCategory(String categoryId) {
        int result = categoryMapper.deleteById(categoryId);
        if(result == 0){
            return new Result(Code.DELETE_ERR, null, "删除失败，受影响行数为0");
        }
        return new Result(Code.DELETE_OK, result);
    }



    // Product
    @Override
    public Result getProductList() {
        List<Product> productList = productMapper.selectList(null);
        if(productList.isEmpty()){
            return new Result(Code.GET_ERR, null, "查询错误，数据库中没有Product列表");
        }
        return new Result(Code.GET_ERR,productList);
    }

    @Override
    public Result getProductById(String productId) {
        Product product = productMapper.selectById(productId);
        if(product == null){
            return  new Result(Code.GET_ERR, null, "查询错误，数据库中没有id为"+productId+"的product数据");
        }
        return new Result(Code.GET_OK, product);
    }

    @Override
    public Result getProductByCategoryId(String categoryId) {
        QueryWrapper<Product> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("category", categoryId);
        List<Product> productList = productMapper.selectList(queryWrapper);
        if(productList.isEmpty()){
            return new Result(Code.GET_ERR, null, "查询失败，数据库中没有category为"+categoryId+"的数据");
        }
        return new Result(Code.GET_OK, productList);
    }

    @Override
    public Result insertProduct(Product product) {
        int result = productMapper.insert(product);
        if(result == 0){
            return new Result(Code.SAVE_ERR, null, "添加失败，受影响行数为0");
        }
        return new Result(Code.SAVE_OK, result);
    }

    @Override
    public Result updateProduct(Product product) {
        int result = productMapper.updateById(product);
        if(result == 0){
            return new Result(Code.UPDATE_ERR, null, "更新失败，受影响行数为0");
        }
        return new Result(Code.UPDATE_OK, result);
    }

    @Override
    public Result deleteProduct(String productId) {
        int result = productMapper.deleteById(productId);
        if(result == 0){
            return new Result(Code.DELETE_ERR, null, "删除失败，受影响行数为0");
        }
        return new Result(Code.DELETE_OK, result);
    }


    // Item
    @Override
    public Result getItemList() {
        List<Item> itemList = itemMapper.selectList(null);
        if(itemList.isEmpty()){
            return new Result(Code.GET_ERR, null, "查询错误，数据库中没有Item列表");
        }
        return new Result(Code.GET_OK, itemList);
    }

    @Override
    public Result getItemById(String itemId) {
        Item item = itemMapper.selectById(itemId);
        if(item == null){
            return new Result(Code.GET_ERR, null, "查询错误，数据库中没有id为"+itemId+"的数据");
        }
        return new Result(Code.GET_OK, item);
    }

    @Override
    public Result getItemByCategoryId(String categoryId) {
        QueryWrapper<Product> productQueryWrapper=new QueryWrapper<>();
        productQueryWrapper.eq("category", categoryId);
        List<Product> productList = productMapper.selectList(productQueryWrapper);

        QueryWrapper<Item> itemQueryWrapper = new QueryWrapper<>();
        List<Item> itemList = new ArrayList<>();
        for(Product product:productList){
            itemQueryWrapper.eq("productid", product.getProductId());
            itemList.addAll(itemMapper.selectList(itemQueryWrapper));
        }

        if(itemList.isEmpty()){
            return new Result(Code.GET_ERR, null, "查询错误，数据库中没有category为"+categoryId+"的数据");
        }
        return new Result(Code.GET_OK, itemList);
    }

    @Override
    public Result getItemByProductId(String productId) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid", productId);
        List<Item> itemList = itemMapper.selectList(queryWrapper);
        if(itemList.isEmpty()){
            return new Result(Code.GET_ERR, null, "查询错误，数据库中没有product为"+productId+"的数据");
        }
        return new Result(Code.GET_OK, itemList);
    }

    @Override
    public Result insertItem(Item item) {
        int result = itemMapper.insert(item);
        if(result == 0){
            return new Result(Code.SAVE_ERR, null, "插入错误，影响行数为0");
        }
        return new Result(Code.SAVE_OK, result);
    }

    @Override
    public Result updateItem(Item item) {
        int result = itemMapper.updateById(item);
        if(result == 0){
            return new Result(Code.UPDATE_ERR, null, "更新错误，影响行数为0");
        }
        return new Result(Code.UPDATE_OK, result);
    }

    @Override
    public Result deleteItem(String itemId) {
        int result = itemMapper.deleteById(itemId);
        if(result == 0){
            return new Result(Code.GET_ERR, null, "删除错误，影响行数为0");
        }
        return new Result(Code.GET_OK, result);
    }


    // 模糊搜索
    @Override
    public Result categorySearch(String keyword){
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.like("name", keyword);
        List<Category> categoryList = categoryMapper.selectList(categoryQueryWrapper);

        if(categoryList.isEmpty()){
            return new Result(Code.GET_ERR, null, "查询错误，数据库中没有关键字为"+keyword+"的item项");
        }

        List<Item> itemList = new ArrayList<>();
        QueryWrapper<Product> productQueryWrapper =  new QueryWrapper<>();
        for(Category category:categoryList){
            productQueryWrapper.eq("category", category.getCategoryId());
            List<Product> productList = productMapper.selectList(productQueryWrapper);
            QueryWrapper<Item> itemQueryWrapper = new QueryWrapper<>();
            for(Product product:productList){
                itemQueryWrapper.eq("productid", product.getProductId());
                itemList.addAll(itemMapper.selectList(itemQueryWrapper));
            }
        }
        return new Result(Code.GET_OK, itemList);
    }

    @Override
    public Result productSearch(String keyword) {
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.like("name", keyword);
        List<Product> productList = productMapper.selectList(productQueryWrapper);

        if(productList.isEmpty()){
            return new Result(Code.GET_ERR, null, "查询错误， 数据库中没有关键字为"+keyword+"的item项");
        }

        List<Item> itemList = new ArrayList<>();
        QueryWrapper<Item> itemQueryWrapper = new QueryWrapper<>();
        for(Product product:productList){
            itemQueryWrapper.eq("productid", product.getProductId());
            itemList.addAll(itemMapper.selectList(itemQueryWrapper));
        }
        return new Result(Code.GET_OK, itemList);
    }

    @Override
    public Result itemSearch(String keyword) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("itemid", keyword);
        List<Item> itemList = itemMapper.selectList(queryWrapper);
        if(itemList.isEmpty()){
            return new Result(Code.GET_ERR, null, "查询错误，数据库中没有关键字为"+keyword+"的item项");
        }
        return new Result(Code.GET_OK, itemList);
    }

}
